package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.ChangePassword;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.domain.Student;
import com.bbu.javaexam.service.ClassStudentService;
import com.bbu.javaexam.service.impl.StudentServiceImpl;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName StudentController.java
 * @Description TODO
 */
@RestController
public class StudentController {

    @Autowired
    private StudentServiceImpl studentService;

    @Autowired
    private ClassStudentService classStudentService;

    @GetMapping("/students/{page}/{size}/{id}")
    public RespData findAll(@PathVariable Integer page, @PathVariable Integer size, @PathVariable Long id) {
        Page<Student> studentPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("classid", id);
        queryWrapper.select("studentid");
        List list = classStudentService.listObjs(queryWrapper);
        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (list.size() == 0) {
            return RespDataHandler.buildRespData(200, "分页查询所有学生", studentPage);
        }
        lambdaQueryWrapper.in(Student::getStudentId, list);
        studentService.page(studentPage, lambdaQueryWrapper);
        return RespDataHandler.buildRespData(200, "分页查询所有学生", studentPage);
    }

    @GetMapping("/student/{studentId}")
    public RespData findById(@PathVariable("studentId") Integer studentId) {
        Student res = studentService.findById(studentId);
        if (res != null) {
            return RespDataHandler.buildRespData(200, "请求成功", res);
        } else {
            return RespDataHandler.buildRespData(404, "查询的用户不存在", null);
        }
    }

    @DeleteMapping("/student/{studentId}")
    public RespData deleteById(@PathVariable("studentId") Integer studentId) {
        return RespDataHandler.buildRespData(200, "删除成功", studentService.deleteById(studentId));
    }

    @PutMapping("/studentPWD/{studentId}")
    public RespData updatePwd(@RequestBody ChangePassword changePassword, @PathVariable("studentId") Integer studentId) {
        Student student1 = studentService.findById(studentId);
        if ((student1.getPwd()).equals(changePassword.getOldPassword())) {
            student1.setPwd(changePassword.getNewPassword());
            studentService.updatePwd(student1);
            return RespDataHandler.buildRespData(200, "密码更新成功", null);
        } else {
            return RespDataHandler.buildRespData(200, "用户密码错误", null);
        }

    }

    @PutMapping("/student")
    public RespData update(@RequestBody Student student) {
        int res = studentService.update(student);
        if (res != 0) {
            return RespDataHandler.buildRespData(200, "更新成功", res);
        }
        return RespDataHandler.buildRespData(400, "更新失败", res);
    }

    @PostMapping("/student")
    public RespData add(@RequestBody Student student) {
        int res = studentService.add(student);
        if (res == 1) {
            return RespDataHandler.buildRespData(200, "添加成功", null);
        } else {
            return RespDataHandler.buildRespData(400, "添加失败", null);
        }
    }
}
